package com.suitsoft.ut.commons;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.suitsoft.ut.service.ServiceException;
/**
 * word,excel,txt转换成swf。
 * @author Suineg
 *
 */
public class OfficeToSWFUtils {
 
	public int DEFAULT_PORT = 8100;
 
	public String DEFAULT_HOST = "192.168.10.252";
	
	/**
	 * 执行前，请启动openoffice服务
	 * 进入$OO_HOME\program下
	 * 执行soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 
	 * @param xlsfile  
	 * @param targetfile
	 * @throws Exception
	 */
	public void SaveAs(String xlsfile, String targetfile)
			throws Exception {
		File xlsf = new File(xlsfile);
		File targetF = new File(targetfile);
		// 获得文件格式
		DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
		DocumentFormat pdfFormat = formatReg.getFormatByFileExtension("pdf");
		DocumentFormat xlsFormat = null;
		String ext = xlsfile.substring(xlsfile.lastIndexOf("."));
		if(".doc".equals(ext)){
			xlsFormat = formatReg.getFormatByFileExtension("doc");
		}else if(".txt".equals(ext)){
			xlsFormat = formatReg.getFormatByFileExtension("txt");
		}else if(".xls".equals(ext)){
			xlsFormat = formatReg.getFormatByFileExtension("xls");
		}else if(".doc".equals(ext)){
			xlsFormat = formatReg.getFormatByFileExtension("docx");
		}else if(".xlsx".equals(ext)){
			xlsFormat = formatReg.getFormatByFileExtension("xls");
		}else if(".ppt".equals(ext)){
			xlsFormat = formatReg.getFormatByFileExtension("ppt");
		}else if(".pptx".equals(ext)){
			xlsFormat = formatReg.getFormatByFileExtension("pptx");
		}else if(".jpg".equals(ext)){
			xlsFormat = formatReg.getFormatByFileExtension("jgp");
		}
		// stream 流的形式
		InputStream inputStream = new FileInputStream(xlsf);
		OutputStream outputStream = new FileOutputStream(targetF);
 
		/**
		 * 
		 */
		OpenOfficeConnection connection = new SocketOpenOfficeConnection(DEFAULT_HOST,8100);
		System.out.println(connection);
		try {
			
			connection.connect();
			DocumentConverter converter = new OpenOfficeDocumentConverter(
					connection);
 
			System.out.println("inputStream------" + inputStream);
			System.out.println("outputStream------" + outputStream);
			converter.convert(inputStream, xlsFormat, outputStream, pdfFormat);
		} catch (ConnectException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
				connection = null;
			}
		}
	}
	
	/**
	 * office文件转PDF
	 */
	public static String officeToPdf(String path,String newPath){
		try {
			new OfficeToSWFUtils().SaveAs(path, newPath);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("转换失败！"+e.getMessage());
		}
		return newPath;
	}
	
	/**
	 * PDF转SWF
	 * @param ptah
	 * @param newPath
	 */
	public static void pdfToSwf(String ptah,String newPath,String webappPath){
		try {
			String command= webappPath + "/WEB-INF/tools\\SWFTools\\pdf2swf.exe  -t "+ptah+" -o  "+newPath+" -s flashversion=9 ";  
			Runtime.getRuntime().exec("cmd /c "+command);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException("转换失败！"+e.getMessage());
			
		}
	}
	
	/**
	 * office转Swf
	 * @param officePath office文件(带绝对路径的)
	 * @param swfName 生成swf文件名(没有路径 光文件名)
	 * @param webappPath 项目的绝对路径(用来定位转换格式的工具及生成临时文件路径)
	 */
	public static void officeToSwf(String officePath,String swfName,String webappPath){
		String pdfPath = officeToPdf(officePath,webappPath + "/WEB-INF/temp/" + swfName.substring(0,swfName.lastIndexOf(".")) + ".pdf");
		pdfToSwf(pdfPath, webappPath +"/temp/" + swfName,webappPath);
	}
 
	public static void main(String[] args) throws Exception {
		String path1 = "d:/1.doc";
		String path2 = "1.swf";
		officeToSwf(path1,path2,"F:\\workSpace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\hrq");
		System.out.println("ok");
	}
}